POJ2155 Matrix <树套树/二维树状数组>

Problem

Matrix

Time Limit:
Memory Limit:

Description

Given an matrix , whose elements are either or . means the number in the row and column. Initially we have .
We can change the matrix in the following way. Given a rectangle whose upper-left corner is and lower-right corner is , we change all the elements in the rectangle by using “not” operation (if it is a ‘ ’ then change it into ‘ ’ otherwise change it into ‘ ’). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions.

  1. ( ) changes the matrix by using the rectangle whose upper-left corner is and lower-right corner is .
  2. ( ) querys .

Input

The first line of the input is an integer representing the number of test cases. The following X blocks each represents a test case.
The first line of each block contains two numbers and ( , ) representing the size of the matrix and the number of the instructions. The following lines each represents an instruction having the format “ ” or “ ”, which has been described above.

Output

For each querying output one line, which has an integer representing .
There is a blank line between every two continuous test cases.

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1

Sample Output

1
2
3
4
1
0
0
1

标签:线段树套线段树/二维树状数组

Solution

此题最简单的方法是二维树状数组,但因为二维树状数组没太大用,所以练习线段树的树套树。
此题用作树套树的模板题再合适不过。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX_N 1000
using namespace std;
int n, m;
int tr[(MAX_N<<2)+5][(MAX_N<<2)+5];
void modify_y(int v1, int v2, int s, int t, int l, int r) {
if (s >= l && t <= r) {
tr[v1][v2] ^= 1;
return;
}
int mid = s+t>>1;
if (l <= mid) modify_y(v1, v2<<1, s, mid, l, r);
if (r >= mid+1) modify_y(v1, v2<<1|1, mid+1, t, l, r);
}
void modify_x(int v, int s, int t, int x1, int y1, int x2, int y2) {
if (s >= x1 && t <= x2) {
modify_y(v, 1, 1, n, y1, y2);
return;
}
int mid = s+t>>1;
if (x1 <= mid) modify_x(v<<1, s, mid, x1, y1, x2, y2);
if (x2 >= mid+1) modify_x(v<<1|1, mid+1, t, x1, y1, x2, y2);
}
int query_y(int v1, int v2, int s, int t, int pos) {
if (s == t) return tr[v1][v2];
int mid = s+t>>1;
if (pos <= mid) return tr[v1][v2]^query_y(v1, v2<<1, s, mid, pos);
else return tr[v1][v2]^query_y(v1, v2<<1|1, mid+1, t, pos);
}
int query_x(int v, int s, int t, int x, int y) {
if (s == t) return query_y(v, 1, 1, n, y);
int mid = s+t>>1;
if (x <= mid) return query_y(v, 1, 1, n, y)^query_x(v<<1, s, mid, x, y);
else return query_y(v, 1, 1, n, y)^query_x(v<<1|1, mid+1, t, x, y);
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
memset(tr, 0, sizeof(tr));
scanf("%d%d", &n, &m);
while (m--) {
char ch;
cin >> ch;
if (ch == 'C') {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
modify_x(1, 1, n, x1, y1, x2, y2);
}
if (ch == 'Q') {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", query_x(1, 1, n, x, y));
}
}
printf("\n");
}
return 0;
}
------------- Thanks For Reading -------------
0%